package array.medium;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class HandOfStraights_846 {
    public boolean isNStraightHand(int[] hand, int groupSize) {

        Map<Integer, Integer> map = new HashMap<>();
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int i : hand) {
            map.put(i, map.getOrDefault(i, 0) + 1);
            queue.offer(i);
        }
        while (!queue.isEmpty()) {
            Integer poll = queue.poll();
            if (map.get(poll) == 0) {
                continue;
            }
            for (int i = 0; i < groupSize; i++) {
                int cnt = map.getOrDefault(poll + i, 0);
                if (cnt == 0) {
                    return false;
                }
                map.put(poll + i, cnt - 1);
            }
        }
        return true;
    }
}
